package com.tcode.servlet;

import java.io.IOException;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.crypto.URIDereferencer;

import com.tcode.bean.UserBean;

/**
 * Servlet implementation class Index2Servlet
 */
@WebServlet("/biubiu")
public class Index2Servlet extends HttpServlet {
	
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Index2Servlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		
//		System.out.println(request.getParameter("text1"));
//		System.out.println(request.getParameter("text2"));
		String text3=request.getParameter("text3");
		request.setAttribute("text3", text3);
		
		UserBean u = new UserBean();
		u.setName("中文");
		u.setPhone("123456");
		request.getSession().setAttribute("text2", u);
//		Cookie cookie1 = new Cookie("user1","123456");
//		Cookie cookie2 = new Cookie("user2","654321");
//		Cookie cookie3 = new Cookie("text3",text3);
//		response.addCookie(cookie1);
//		response.addCookie(cookie2);
//		response.addCookie(cookie3);
//		cookie1.setMaxAge(60*60*24*7);
		
//		request.getAttribute("text3");
//		request.removeAttribute("text3");
//		ServletContext();
//		response.getWriter().append(text3+"==================").append(request.getContextPath());
//		response.setStatus(404);
//		请求转发
		
		request.getRequestDispatcher("404.jsp").forward(request, response);
//		重定向
//		response.sendRedirect("http://www.qq.com");
		/*String context =request.getRemoteAddr();
		String sessionId =request.getRequestedSessionId();
		String header = request.getHeader("Accept-Encoding");
		
		if("HTTP/1.1".equals(context)){
			
			response.getWriter().append("200-"+context).append(request.getContextPath());
			
		}else{
			
			response.getWriter().append("404-"+context+"-"+sessionId+"-"+header).append(request.getContextPath());
			
		}*/
		
//		response.sendRedirect("http://www.qq.com");
		/*try{
			int i =1/0;
		}catch(Exception e){
			e.printStackTrace();
			response.sendRedirect("http://www.qq.com");
		}*/
		
//		request.getRequestDispatcher("/second.jsp").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
//		response.getWriter().append("biubiubiubiubiu...............");
	}

}
